{% extends 'base.html' %}

{% block content %}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="{{ url_for('home') }}">组卷页面</a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('teacher_dashboard') }}">返回老师面板</a>
                    </li>
                </ul>
            </div>
        </nav>
<div class="container mt-5">
    <h2>组卷页面</h2>

    <!-- 创建试卷表单 -->
    <form method="POST" action="{{ url_for('create_paper') }}">
        <div class="form-group">
            <label for="paperName">试卷名称</label>
            <input type="text" class="form-control" id="paperName" name="paperName" required>
        </div>
          <!-- 触发模态框的按钮 -->
            <button type="button" class="btn btn-secondary" data-toggle="modal" data-target="#paperNameModal">
                随机组卷
            </button>
        <h4>选择题目</h4>
        <div class="row">
            {% for question in questions %}
            <div class="col-md-6 mb-3">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">{{ question[4] }}</h5>
                        <p class="card-text"><strong>题型:</strong> {{ question[3] }}</p>
                        <p class="card-text"><strong>内容:</strong> {{ question[5] }}</p>
                        <p class="card-text"><strong>答案:</strong> {{ question[6] }}</p>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="question{{ question[0] }}"
                                name="selected_questions" value="{{ question[0] }}">
                            <label class="form-check-label" for="question{{ question[0] }}">
                                选择此题目
                            </label>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>

        <div class="mt-3">
            <button type="submit" class="btn btn-primary">创建试卷</button>

        </div>
    </form>
</div>

<!-- 模态框 -->
<div class="modal fade" id="paperNameModal" tabindex="-1" role="dialog" aria-labelledby="paperNameModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form id="randomPaperForm" method="POST" action="{{ url_for('random_paper') }}">
                <div class="modal-header">
                    <h5 class="modal-title" id="paperNameModalLabel">输入试卷名称</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="randomPaperNameInput">试卷名称</label>
                        <input type="text" class="form-control" id="randomPaperNameInput" name="paperName" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary" id="submitRandomPaperBtn">生成随机试卷</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        // 监听随机试卷表单的提交事件
        $('#randomPaperForm').submit(function (event) {
            event.preventDefault(); // 阻止表单的默认提交行为

            // 使用AJAX提交表单数据
            $.ajax({
                type: 'POST',
                url: $(this).attr('action'),
                data: $(this).serialize(), // 序列化表单数据
                success: function (response) {
                    // 处理成功响应，例如显示成功消息
                    $('#paperNameModal').modal('hide'); // 隐藏模态框
                    alert('随机试卷创建成功！'); // 可以用更好的方式显示成功消息
                    location.reload(); // 重新加载页面或执行其他操作
                },
                error: function (xhr, status, error) {
                    // 处理错误响应，例如显示错误消息
                    alert('生成随机试卷时出现错误：' + error);
                }
            });
        });
    });
</script>

{% endblock %}
